package com.seu.multithread.demo02;

/**
 * @author liangfeihu
 * @since 2019/2/18
 */
public class MyThreadNumPrinter implements Runnable {
    private int startNum;
    private Object lock;

    public MyThreadNumPrinter(int startNum, Object lock) {
        this.startNum = startNum;
        this.lock = lock;
    }


    @Override
    public void run() {
        int totalTimes = 10;
        while (totalTimes > 0) {
            synchronized (lock) {
                System.out.print(startNum + " ");
                startNum += 2;
                totalTimes--;

                lock.notify();

                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        } // end while
    }

    public static void main(String[] args) throws Exception{
        Object lock = new Object();
        MyThreadNumPrinter numPrinterA = new MyThreadNumPrinter(1, lock);
        MyThreadNumPrinter numPrinterB = new MyThreadNumPrinter(2, lock);

        new Thread(numPrinterA).start();
        Thread.sleep(100);

        new Thread(numPrinterB).start();
        Thread.sleep(1000);
    }
}
